#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#define BLINKER_MIOT_MULTI_OUTLET

#include "led.h"
#include <Blinker.h>


#define BUTTON_0 "btn0"
#define BUTTON_1 "btn1"
#define BUTTON_2 "btn2"
#define BUTTON_3 "btn3"
#define BUTTON_4 "btn4"

BlinkerButton Button0(BUTTON_0);
BlinkerButton Button1(BUTTON_1);
BlinkerButton Button2(BUTTON_2);
BlinkerButton Button3(BUTTON_3);
BlinkerButton Button4(BUTTON_4);

BlinkerButton button_arr[5] = {Button0, Button1, Button2, Button3, Button4};

uint32_t offline_millis = 0;//掉线时间戳
bool offline_flag=false;//掉线触发标志

String color_on = "#8B0000";
String color_off = "#008000";

// 0 表示的总开关
bool oState[5] = {false}; // 0 1 2 3 4

void set_button_power_state(const String &state, int pin)
{
    if (state == BLINKER_CMD_ON)
    {
        oState[pin] = true;
        button_arr[pin].print("on");
        button_arr[pin].color(color_on);
        button_arr[pin].text("已打开");
        BlinkerMIOT.powerState("on", pin);
        BlinkerMIOT.print();
        digitalWrite(pin_arr[pin - 1], HIGH);
        notify_state_change(pin, 1);
    }
    else if (state == BLINKER_CMD_OFF)
    {
        oState[pin] = false;
        button_arr[pin].print("off");
        button_arr[pin].color(color_off);
        button_arr[pin].text("已关闭");
        BlinkerMIOT.powerState("off", pin);
        BlinkerMIOT.print();
        digitalWrite(pin_arr[pin - 1], LOW);
        notify_state_change(pin, 0);
    }
}

void miotPowerState(const String &state, uint8_t num)
{
    BLINKER_LOG("need set outlet: ", num, ", power state: ", state);

    if (state == BLINKER_CMD_ON)
    {

        if (num >= 1)
        {
            oState[num] = true;
            digitalWrite(pin_arr[num - 1], HIGH);
            notify_state_change(num, 1);
            BlinkerMIOT.powerState("on", num);
            BlinkerMIOT.print();
            button_arr[num].print("on");
            button_arr[num].color(color_on);
            button_arr[num].text("已打开");
        }
        else if (num == 0)
        {
            for (size_t i = 0; i < 5; i++)
            {
                oState[i] = true;
                Button0.text("已打开");
                button_arr[i].print("on");
                button_arr[i].color(color_on);
                BlinkerMIOT.powerState("on", i);
                BlinkerMIOT.print();
                if (i >= 1)
                {
                    digitalWrite(pin_arr[i - 1], HIGH);
                    notify_state_change(i, 1);
                }
            }
        }
    }
    else if (state == BLINKER_CMD_OFF)
    {
        if (num >= 1)
        {
            oState[num] = false;
            digitalWrite(pin_arr[num - 1], LOW);
            notify_state_change(num, 0);
            BlinkerMIOT.powerState("off", num);
            BlinkerMIOT.print();
            button_arr[num].print("off");
            button_arr[num].color(color_off);
            button_arr[num].text("已关闭");
        }
        else if (num == 0)
        {
            for (size_t i = 0; i < 5; i++)
            {
                oState[i] = false;
                Button0.text("已关闭");
                button_arr[i].print("off");
                button_arr[i].color(color_off);
                BlinkerMIOT.powerState("off", i);
                BlinkerMIOT.print();
                if (i >= 1)
                {
                    digitalWrite(pin_arr[i - 1], LOW);
                    notify_state_change(i, 0);
                }
            }
        }
    }
}

void miotQuery(int32_t queryCode, uint8_t num)
{
    BLINKER_LOG("MIOT Query outlet: ", num, ", codes: ", queryCode);

    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("MIOT Query All");
        BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
        BLINKER_LOG("MIOT Query Power State");
        BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
        BlinkerMIOT.print();
        break;
    default:
        BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
        BlinkerMIOT.print();
        break;
    }
}

void Button0attach(const String &state)
{
    if (state == BLINKER_CMD_ON)
    {
        for (size_t i = 0; i < 5; i++)
        {
            oState[i] = true;
            Button0.text("已打开");
            button_arr[i].print("on");
            button_arr[i].color(color_on);
            BlinkerMIOT.powerState("on", i);
            BlinkerMIOT.print();
            if (i >= 1)
            {
                digitalWrite(pin_arr[i - 1], HIGH);
                notify_state_change(i, 1);
            }
        }
    }
    else if (state == BLINKER_CMD_OFF)
    {
        for (size_t i = 0; i < 5; i++)
        {
            oState[i] = false;
            Button0.text("已关闭");
            button_arr[i].print("off");
            button_arr[i].color(color_off);
            BlinkerMIOT.powerState("off", i);
            BlinkerMIOT.print();
            if (i >= 1)
            {
                digitalWrite(pin_arr[i - 1], LOW);
                notify_state_change(i, LOW);
            }
        }
    }
}
void Button1attach(const String &state)
{
    set_button_power_state(state, 1);
}
void Button2attach(const String &state)
{
    set_button_power_state(state, 2);
}
void Button3attach(const String &state)
{
    set_button_power_state(state, 3);
}
void Button4attach(const String &state)
{
    set_button_power_state(state, 4);
}

void dataRead(const String &data)
{
    BLINKER_LOG("Blinker readString: ", data);
    ;

    uint32_t BlinkerTime = millis();

    Blinker.print("millis", BlinkerTime);
}

void LED_init()
{

    BLINKER_DEBUG.stream(Serial);
    Blinker.begin(auth);
    Blinker.attachData(dataRead);
    oState[0] = true;
    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachQuery(miotQuery);
    Button0.attach(Button0attach);
    Button1.attach(Button1attach);
    Button2.attach(Button2attach);
    Button3.attach(Button3attach);
    Button4.attach(Button4attach);
}

void LED_run()
{
    Blinker.run();

    if(Blinker.connect()) 
    {   
        offline_millis=0;offline_flag=false;
    }
    else if(!Blinker.connected()&&millis()>180000)
    {
        offline_flag=true;
    }
    if(millis()>180000&&!offline_millis&&offline_flag)
    {   
        offline_flag=false;
        offline_millis=millis();
    }
    if(millis()>180000&&offline_millis&&millis()-offline_millis>=180000)
    {
    BLINKER_LOG_ALL(BLINKER_F("******************************************Reatart***************************************"));//串口打印重启消息

        ESP.restart();//ESP32 freertos重启
    }
}

void inspect_pin_state(int pin, bool state)
{
    if (Blinker.connect())
    {
        button_arr[pin + 1].print(state ? "on" : "off");
        button_arr[pin + 1].color(state ? color_on : color_off);
        button_arr[pin + 1].text(state ? "已打开" : "已关闭");
        BlinkerMIOT.powerState(state ? "on" : "off", pin + 1);
        BlinkerMIOT.print();
    }
}